import QtQuick  2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

Dialog {
    id: root
    title: qsTr("发现新版本")
    modal: true
    standardButtons: Dialog.Ok | Dialog.Cancel
    width: 500
    height: 350

    property string currentVersion: ""
    property string latestVersion: ""
    property string releaseNotes: ""
    property bool forceUpdate: false
    property bool isDownloading: false
    property int downloadProgress: 0

    onAccepted: {
        root.open() // 打开对话框
        if (forceUpdate) {
            // 强制更新时，直接开始下载
            updateChecker.overwriteExisting = true
            updateChecker.downloadUpdate()
        } else {
            // 非强制更新时，设置不覆盖已存在的文件
            updateChecker.overwriteExisting = false
            updateChecker.downloadUpdate()
        }
    }

    onRejected: {
        if (isDownloading) {
            updateChecker.cancelDownload()
        }
        if (forceUpdate) { // 强制更新时，不允许取消
            root.open()
        } else {
            // 非强制更新时，关闭对话框            
            root.close()
        }
    }

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 0
        spacing: 4

        Label {
            text: qsTr("当前版本: %1").arg(currentVersion)
            font.pixelSize: 14
        }

        Label {
            text: qsTr("最新版本: %1").arg(latestVersion)
            font.pixelSize: 14
            font.bold: true
        }

        Label {
            text: qsTr("更新说明:")
            font.pixelSize: 14
            font.bold: true
        }

        ScrollView {
            Layout.fillWidth: true
            Layout.fillHeight: true
            clip: true

            TextArea {
                id: releaseNotesText
                text: releaseNotes
                wrapMode: Text.WordWrap
                readOnly: true
                background: Rectangle {
                    color: "transparent"
                }
            }
        }

        ProgressBar {
            id: downloadProgressBar
            Layout.fillWidth: true
            visible: isDownloading
            value: downloadProgress / 100
            from: 0
            to: 1
        }

        Label {
            text: isDownloading ? qsTr("正在下载更新... %1%").arg(downloadProgress) : ""
            visible: isDownloading
            Layout.alignment: Qt.AlignHCenter
        }
    }
} 